<?php

require_once(TEST_INTEGRATION_PATH . 'OrderFactory.php');
require_once(TEST_DTO_PATH.'ProductDTO.php');
require_once(TEST_DTO_PATH.'ProductOptionDTO.php');
require_once(TEST_DTO_PATH.'CashierDTO.php');
require_once(TEST_DTO_PATH.'OrderDTO.php');

class TestOfOrderWithAttributes extends UnitTestCase {
	private $db;
	private $order;
	private $productDTO;
	private $productOptionDTO;
	private $orderDTO;
	
	function __construct() {
		$this->db = new Datasource(TEST_DB_SERVER, TEST_DB_DATABASE, TEST_DB_SERVER_USERNAME, TEST_DB_SERVER_PASSWORD);
		
		$this->createSchema();
		
		$option1 = new ProductOptionDTO;
		$option1->options_id = 3;
		$option1->options_values_id = 5;
		//$this->option1->options_values_name = "Value";		
		
		$option2 = new ProductOptionDTO;
		$option2->options_id = 4;
		$option2->options_values_id = 1;
		//$this->option2->options_values_name = "4 mb";
		
		$productDTO = new ProductDTO;
		$productDTO->pid = 1;
		$productDTO->options = array($option1, $option2);
		
		$cashierDTO = new CashierDTO;
		$cashierDTO->name = "Donna";
		
		$this->orderDTO = new OrderDTO();
		$this->orderDTO->products = array($productDTO);
		$this->orderDTO->price = 299.99;
		$this->orderDTO->currency = "USD";
		$this->orderDTO->method = "cod";
		$this->orderDTO->cashier = $cashierDTO;
		
		$this->order = new OrderFactory();
	}
	
	private function createSchema() {
		/* This order has been made via the Zen Cart Front-End
		 *  to have a direct comparisson of values.
		 *  Note that the order status is 3 (delivered) */
		$this->db->execute("INSERT INTO orders (orders_id ,customers_id ,customers_name ,customers_company ,customers_street_address ,customers_suburb ,customers_city ,customers_postcode ,customers_state ,customers_country ,customers_telephone ,customers_email_address ,customers_address_format_id ,delivery_name ,delivery_company ,delivery_street_address ,delivery_suburb ,delivery_city ,delivery_postcode ,delivery_state ,delivery_country ,delivery_address_format_id ,billing_name ,billing_company ,billing_street_address ,billing_suburb ,billing_city ,billing_postcode ,billing_state ,billing_country ,billing_address_format_id ,payment_method ,payment_module_code ,shipping_method ,shipping_module_code ,coupon_code ,cc_type ,cc_owner ,cc_number ,cc_expires ,last_modified ,date_purchased ,orders_status ,orders_date_finished ,currency ,currency_value ,order_total ,order_tax ,paypal_ipn_id ,ip_address) VALUES ('1', '1', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '12345', 'root@localhost', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Bill Smith', 'JustaDemo', '123 Any Avenue', '', 'Here', '12345', 'California', 'United States', '2', 'Cash on Delivery', 'cod', 'Store Pickup (Walk In)', 'storepickup', '', '', '', '', '', NULL , '2008-01-30 16:15:56', '3', NULL , 'USD', '1.000000', '299.99', '0.00', '0', '127.0.0.1 - 127.0.0.1' )");
		$this->db->execute("INSERT INTO orders_total (orders_total_id ,orders_id ,title ,text ,value ,class ,sort_order) VALUES ('1', '1', 'Sub-Total:', '$299.99', '299.9900', 'ot_subtotal', '100')");
		$this->db->execute("INSERT INTO orders_total (orders_total_id ,orders_id ,title ,text ,value ,class ,sort_order) VALUES ('2', '1', 'Store Pickup (Walk In):', '$0.00', '0.0000', 'ot_shipping', '200')");
		$this->db->execute("INSERT INTO orders_total (orders_total_id ,orders_id ,title ,text ,value ,class ,sort_order) VALUES ('3', '1', 'Total:', '$299.99', '299.9900', 'ot_total', '999')");
		$this->db->execute("INSERT INTO orders_status_history (orders_status_history_id ,orders_id ,orders_status_id ,date_added ,customer_notified ,comments) VALUES ('1', '1', '1', '2008-01-30 16:15:56', '1', 'Special Instruction')");
		$this->db->execute("INSERT INTO orders_status_history (orders_status_history_id ,orders_id ,orders_status_id ,date_added ,customer_notified ,comments) VALUES ('2', '1', '3', '2008-02-05 12:34:01', '1', '') ");		
		$this->db->execute("INSERT INTO orders_products VALUES ('1', '1', '1', 'MG200MMS', 'Matrox G200 MMS', '299.9900', '299.9900', '0.0000', '1', '0.0000', '0', '0', '0', '0', '1:edff669e5da95c1d027b04d5412532fa')");		
		$this->db->execute("INSERT INTO orders_products_attributes (orders_products_attributes_id ,orders_id ,orders_products_id ,products_options ,products_options_values ,options_values_price ,price_prefix ,product_attribute_is_free ,products_attributes_weight ,products_attributes_weight_prefix ,attributes_discounted ,attributes_price_base_included ,attributes_price_onetime ,attributes_price_factor ,attributes_price_factor_offset ,attributes_price_factor_onetime ,attributes_price_factor_onetime_offset ,attributes_qty_prices ,attributes_qty_prices_onetime ,attributes_price_words ,attributes_price_words_free ,attributes_price_letters ,attributes_price_letters_free ,products_options_id ,products_options_values_id ,products_prid) VALUES ('1', '1', '1', 'Model', 'Value', '0.0000', '+', '0', '0', '', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '3', '5', '1:edff669e5da95c1d027b04d5412532fa') ");
		$this->db->execute("INSERT INTO orders_products_attributes (orders_products_attributes_id ,orders_id ,orders_products_id ,products_options ,products_options_values ,options_values_price ,price_prefix ,product_attribute_is_free ,products_attributes_weight ,products_attributes_weight_prefix ,attributes_discounted ,attributes_price_base_included ,attributes_price_onetime ,attributes_price_factor ,attributes_price_factor_offset ,attributes_price_factor_onetime ,attributes_price_factor_onetime_offset ,attributes_qty_prices ,attributes_qty_prices_onetime ,attributes_price_words ,attributes_price_words_free ,attributes_price_letters ,attributes_price_letters_free ,products_options_id ,products_options_values_id ,products_prid) VALUES ('2', '1', '1', 'Memory', '4 mb', '0.0000', '', '0', '0', '', '1', '1', '0.0000', '0.0000', '0.0000', '0.0000', '0.0000', '', '', '0.0000', '0', '0.0000', '0', '4', '1', '1:edff669e5da95c1d027b04d5412532fa') ");
	}
	
	private function dropSchema() {
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 1");
		$this->db->execute("DELETE FROM `orders` WHERE `orders_id` = 2");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 1");
		$this->db->execute("DELETE FROM `orders_total` WHERE `orders_id` = 2");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 1");
		$this->db->execute("DELETE FROM `orders_status_history` WHERE `orders_id` = 2");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 1");
		$this->db->execute("DELETE FROM `orders_products` WHERE `orders_id` = 2");
		$this->db->execute("DELETE FROM `orders_products_attributes` WHERE `orders_id` = 1");
		$this->db->execute("DELETE FROM `orders_products_attributes` WHERE `orders_id` = 2");
	}
	
	function setUp() {
		//$this->createSchema();
	}
	
	function tearDown() {
		//$this->dropSchema();
	}

	function testOrderExecute() {
		$this->assertTrue($this->order->execute($this->orderDTO));
	}
	
	function testOrdersTable() {
		// comparing all fields except the orders_id, date_purchased, and ip_address
		$originalorder =	$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =1");
		$neworder =			$this->db->execute(" SELECT `customers_id` , `customers_name` , `customers_company` , `customers_street_address` , `customers_suburb` , `customers_city` , `customers_postcode` , `customers_state` , `customers_country` , `customers_telephone` , `customers_email_address` , `customers_address_format_id` , `delivery_name` , `delivery_company` , `delivery_street_address` , `delivery_suburb` , `delivery_city` , `delivery_postcode` , `delivery_state` , `delivery_country` , `delivery_address_format_id` , `billing_name` , `billing_company` , `billing_street_address` , `billing_suburb` , `billing_city` , `billing_postcode` , `billing_state` , `billing_country` , `billing_address_format_id` , `payment_method` , `payment_module_code` , `shipping_method` , `shipping_module_code` , `coupon_code` , `cc_type` , `cc_owner` , `cc_number` , `cc_expires` , `cc_cvv` , `last_modified` , `orders_status` , `orders_date_finished` , `currency` , `currency_value` , `order_total` , `order_tax` , `paypal_ipn_id` FROM `orders` WHERE `orders_id` =2");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsTable() {
		// comparing all fields except orders_id and products_prid
		$originalorder =	$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` FROM orders_products WHERE `orders_products_id` =1");
		$neworder =			$this->db->execute("SELECT `products_id` , `products_model` , `products_name` , `products_price` , `final_price` , `products_tax` , `products_quantity` , `onetime_charges` , `products_priced_by_attribute` , `product_is_free` , `products_discount_type` , `products_discount_type_from` FROM orders_products WHERE `orders_products_id` =2");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersTotalTable() {
		// comparing only ot_total without the fields orders_total_id and orders_id
		$originalorder =	$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =1 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$neworder = 		$this->db->execute("SELECT `title` , `text` , `value` , `class` , `sort_order` FROM `orders_total` WHERE `orders_id` =2 AND `class` LIKE 'ot_total' LIMIT 0 , 1 ");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsAttributesTable() {
		$originalorder =	$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =1");
		$neworder = 		$this->db->execute("SELECT `products_options` , `products_options_values` , `options_values_price` , `price_prefix` , `product_attribute_is_free` , `products_attributes_weight` , `products_attributes_weight_prefix` , `attributes_discounted` , `attributes_price_base_included` , `attributes_price_onetime` , `attributes_price_factor` , `attributes_price_factor_offset` , `attributes_price_factor_onetime` , `attributes_price_factor_onetime_offset` , `attributes_qty_prices` , `attributes_qty_prices_onetime` , `attributes_price_words` , `attributes_price_words_free` , `attributes_price_letters` , `attributes_price_letters_free` , `products_options_id` , `products_options_values_id` FROM `orders_products_attributes` WHERE `orders_id` =2");
		$this->assertEqual(mysql_fetch_array($originalorder), mysql_fetch_array($neworder));
	}
	
	function testOrdersProductsQuantityReduction() {
		// The Matrox Product has a quantity of 31 at the start.
		$newquantity = $this->db->execute(" SELECT `products_quantity` FROM `products` WHERE products_id =1");
		$this->assertEqual(30, (int)mysql_result($newquantity, "products_quantity"));
		//$this->dropSchema();
	}
}

?>
